import { defineStore } from 'pinia'
import { req_shopCarList } from '../api/req_fun'

export default defineStore('ShopCar', {
    state: () => ({
        list: [],
        courses: [],
        payModes: []
    }),
    actions: {
        async get_shopCarList() {
            try {
                const { data } = await req_shopCarList()
                // data.data.list.forEach((item) => {
                //     item.checked = false
                // })
                this.list = data.data.list
            } catch (err) {
                console.log(err);
            }
        }
    },
    getters: {
        sumPrice(store) {
            return store.list.reduce((PreSum, item, index) => {
                if (item.checked && (item.checked === true)) {
                    return PreSum + (item.counter * item.salePrice)
                } else {
                    return PreSum
                }
            }, 0)
        },
        sumConter(store) {
            return store.list.reduce((PreSum, item, index) => {
                if (item.checked && (item.checked === true)) {
                    return PreSum + item.counter
                } else {
                    return PreSum
                }
            }, 0)
        }
    },
    persist: {
        //这里存储默认使用的是session
        enabled: true,
        strategies: [
            {
                //key的名称
                key: 'shopCar',
                //更改默认存储，我更改为localStorage
                storage: localStorage,
                // 可以选择哪些进入local存储，这样就不用全部都进去存储了
                // 默认是全部进去存储
                paths: ['courses']
            }
        ]
    }
})